import '../config/index'
import Cookies from 'vue-cookie'

const TokenKey = window.SITE_CONFIG.tokenKey === undefined ? 'token' : window.SITE_CONFIG.tokenKey

export function getTokenKey() {
  return TokenKey
}

export function getToken() {
  const token = Cookies.get(TokenKey)
  return token == null ? sessionStorage.getItem(TokenKey) : token
}

export function setToken(token) {
  sessionStorage.setItem(TokenKey, token)
  const path = window.SITE_CONFIG.baseUrl.substring(window.SITE_CONFIG.baseUrl.lastIndexOf('/'), window.SITE_CONFIG.baseUrl.length)
  return Cookies.set(TokenKey, token, '30MIN', path)
}

export function removeToken() {
  sessionStorage.removeItem(TokenKey)
  return Cookies.delete(TokenKey)
}
